﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;
using Exhibition.Common.Extension;
using Exhibition.Common.Helper;
namespace Exhibition.Manage
{
    public partial class Index : BaseAdminPage
    {
        public string strMenu = string.Empty;
        StringBuilder sb = new StringBuilder();
        List<FuncModel> mens = new List<FuncModel>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CurrentSite != null) {
                ltrName.Text = CurrentSite.WebName;
            }

            if (AdminUser != null)
            {

                ltrName.Text = AdminUser.UserName;
                if (AdminUser.UserName == "admin")
                {
                    switchSite.Visible = true;
                }
                else {
                    switchSite.Visible = false;
                }
            }
            if (!IsPostBack) {
               
                LoadMenus();
            }

        }

       

        private void LoadMenus()
        {
            GetMenus(0);
            List<Menus> mes = mens.Select(s => new Menus()
            {
                menuname = s.FuncName,
                menuid = s.Id,
                icon = s.Icon,
                url = s.FuncPath,
                menus = s.SonModels.Select(m => new Menus() { icon = m.Icon, menuname = m.FuncName, menuid = m.Id, url = m.FuncPath }).ToList()
            }).ToList();
            strMenu = sb.Append(mes.ToJsonItem()).ToString();
        }

        public void GetMenus(int pid)
        {
            // "menuid": "1",
        //        "icon": "icon-sys",
        //        "menuname": "系统设置",
        //        "menus": [{
        //            "menuid": "12",
        //            "menuname": "角色管理",
        //            "icon": "icon-nav",
        //            "url": "UserRoleList.aspx" 
        //        },
            List<FuncModel> models = CenterService.funcService.GetListByPid(pid);
            //mens.AddRange(models);
            foreach (var mdl in models)
            {

                mdl.SonModels = GetSonMenus(mdl.Id);
                //超级管理员默认所有权限
                if (AdminUser != null)
                {
                    if (AdminUser.UserName.ToLower().Equals("admin"))
                    {
                        mens.Add(mdl);
                    }

                    //一般用户权限
                    if (AdminUser.UserSysRight != null)
                    {
                        if (AdminUser.UserSysRight.IndexOf(mdl.FuncCode) >= 0)
                        {
                            mens.Add(mdl);
                        }
                    }
                }


            }
            //return mens;
            //List<Menus> menus = models.Select(s => new Menus() {
            //    menuname=s.FuncName,
            //    menuid=s.Id,
            //    icon=s.Icon,
            //    url=s.FuncPath
            //}).ToList() ;
            //sb.Append(menus.ToJsonItem());
            //foreach (var mdl in models)
            //{
            //    sb.Append("{");
            //    sb.AppendFormat("\"menuid:\"{0},", mdl.Id);
            //    sb.AppendFormat("\"icon:\"{0},",mdl.Icon);
            //    sb.AppendFormat("\"menuname:\"{0},",mdl.FuncName);
            //    if (mdl.ParentId == 0)
            //    {
            //        sb.Append("\"menus:\"[");
            //        GetMenus(mdl.Id);
            //        sb.Append("]");
            //    }
            //    sb.Append("},");
                
            //}


        }

        private List<FuncModel> GetSonMenus(int p)
        {
            List<FuncModel> models = CenterService.funcService.GetListByPid(p);
            //mens.AddRange(models);
            //foreach (var mdl in models)
            //{

            //    mdl.SonModels = GetSonMenus(mdl.Id);
            //    mens.Add(mdl);

            //}
            return models;
        }
    }
}